﻿using Microsoft.SPOT;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.PeteBrown;

namespace GadgeteerMidiLoopbackTest
{
    public partial class Program
    {
        private byte _note = 33;
        private byte _velocity = 127;
        private MidiChannel _channel = MidiChannel.Channel01;


        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");


            midi.MessageReceived += midi_MessageReceived;
            midi.EnableReceiver();

            button.ButtonPressed += button_ButtonPressed;
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            Debug.Print("Button Pressed");
            midi.SendNoteOn(_channel, _note, _velocity);
            Debug.Print("MIDI Message Sent ============== ");
        }

        void midi_MessageReceived(MidiModule sender, MidiMessage message)
        {
            Debug.Print("MIDI Message Received ============== ");

            if (message.Channel != _channel)
                Debug.Print("Incorrect Channel Received: " + message.Channel);
            else
                Debug.Print("Correct channel received.");

            if (message.DataByte1 != _note)
                Debug.Print("Incorrect note received: " + message.DataByte1);
            else
                Debug.Print("Correct note received.");

            if (message.DataByte2 != _velocity)
                Debug.Print("Incorrect velocity received: " + message.DataByte2);
            else
                Debug.Print("Correct velocity received.");

        }
    }
}
